// utils is a library of generic helper functions non-specific to axios

const toString = Object.prototype.toString;

export function is(val, type) {
	return toString.call(val) === `[object ${type}]`;
}

/**
 * Determine if a value is an Array
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an Array, otherwise false
 */
export function isArray(val) {
	return val && Array.isArray(val);
}

/**
 * Determine if a value is an Object
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is an Object, otherwise false
 */
export function isObject(val) {
	return val !== null && is(val, 'Object');
}

/**
 * Determine if a value is a Date
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a Date, otherwise false
 */
export function isDate(val) {
	return is(val, 'Date');
}

/**
 * Determine if a value is a URLSearchParams object
 *
 * @param {Object} val The value to test
 * @returns {boolean} True if value is a URLSearchParams object, otherwise false
 */
export function isURLSearchParams(val) {
	return is(val, 'URLSearchParams');
}

/**
 * Iterate over an Array or an Object invoking a function for each item.
 *
 * If `obj` is an Array callback will be called passing
 * the value, index, and complete array for each item.
 *
 * If 'obj' is an Object callback will be called passing
 * the value, key, and complete object for each property.
 *
 * @param {Object|Array} obj The object to iterate
 * @param {Function} fn The callback to invoke for each item
 */
export function forEach(obj, fn) {
	// Don't bother if no value provided
	if (obj === null || typeof obj === 'undefined') {
		return;
	}

	// Force an array if not already something iterable
	if (typeof obj !== 'object') {
		obj = [obj];
	}

	if (isArray(obj)) {
		// Iterate over array values
		obj.forEach((val, index, curObj) => {
			fn.call(null, val, index, curObj);
		});
	} else {
		// Iterate over object keys
		for (const key in obj) {
			if (Object.prototype.hasOwnProperty.call(obj, key)) {
				fn.call(null, obj[key], key, obj);
			}
		}
	}
}

/**
 * 是否为boolean 值
 * @param val
 * @returns {boolean}
 */
export function isBoolean(val) {
	return is(val, 'Boolean');
}

/**
 * 是否为真正的对象{} new Object
 * @param {any} obj - 检测的对象
 * @returns {boolean}
 */
export function isPlainObject(obj) {
	return Object.prototype.toString.call(obj) === '[object Object]';
}

/**
 * Function equal to merge with the difference being that no reference
 * to original objects is kept.
 *
 * @see merge
 * @param {Object} obj1 Object to merge
 * @returns {Object} Result of all merge properties
 */

export function deepMerge( /* obj1, obj2, obj3, ... */ ) {
	const result = {};

	function assignValue(val, key) {
		if (typeof result[key] === 'object' && typeof val === 'object') {
			result[key] = deepMerge(result[key], val);
		} else if (typeof val === 'object') {
			result[key] = deepMerge({}, val);
		} else {
			result[key] = val;
		}
	}
	for (let i = 0, l = arguments.length; i < l; i++) {
		// eslint-disable-next-line prefer-rest-params
		forEach(arguments[i], assignValue);
	}
	return result;
}

export function isUndefined(val) {
	return typeof val === 'undefined';
}